package NiuKe;

public class binarn_search {
    public static void main(String[] args) {
        int[][] array = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15},{3,21,34,88}};
        int target = 13;

        System.out.println(Find(target, array));


//        int[] nums = {-1,0,3,4,6,10,13,14};
//        int target = 88;
//        System.out.println(search(nums,target));

    }

    //查找二维数组指定数值
    public static boolean Find (int target, int[][] array) {

        boolean isHa = false;

        int row = array.length;
        int com = array[0].length;
        for(int i=0;i<row;i++){
            for(int j=0;j<com;j++){
                if (array[i][j]==target) {
                    isHa = true;
                    return isHa;
                }
            }
        }

        return isHa;
    }

    //查找一维数组指定数值
    public static int search (int[] nums, int target) {
        // write code here
        int index = -1;
        //定义索引-1，如果找不到就返回index
        int temp_max = nums.length;
        //获取数组长度
        int i = 0;
        //定义起始下标

        while (i<temp_max) {
            //如果下标小于指定值，则运行
            int zh =  (temp_max-i)/2 + i;
            //查找数组中间下标值
            if(nums[zh]==target){
                index = zh;
                return index;
            }
            //如果数组中间值等于要找的数值就返回中间值下标
            else if(nums[zh]<target){
                i = 1+ zh;
            }
            //如果比中间值大，那就从下一个值开始为起始下标
            else {
                temp_max = zh;
            }
            //如果比中间值小，那中间值下标就是最终下标

        }
        return index;
    }
}
